// pages/storeShop/storeShop.js
var app = getApp();
var util = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curIndex:0,
        goodsPage:1,
        comPage:1,
        tishi_title: '正在加载中...',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
		console.log(options);
        var pages = getCurrentPages(); 
		wx.getSystemInfo({
			success: function(res) {
				console.log(res);
				that.setData({
					scrollHeight: parseInt(res.windowHeight) - parseInt(128)
				});
				console.log(res.windowHeight - 128);
				console.log(that.data.scrollHeight);
			},
		})
    },
    getStoreInfo:function(){
        var that = this;
        util.showLoading();
        var apiUrl = '/api/store/store/index';
        var apiData = {
        }
        util.fetchPost(apiUrl,apiData,function(res){
            util.hideLoading();
            if(res.state == 1){
                that.setData({
                    store_info:res.datas
                });
            }else{
                util.message(res.datas);
            }
        });
    },
    getClassList:function(){
        var that = this;
        util.fetchPost("/api/store/store/goods_class_list", {
            store_id: that.data.id,
        }, function (res) {
            if (res.state == 1) {
                that.setData({
                    classlist: res.datas,
                    classid: res.datas[0].id,
                })
                that.getGoodsList();
            } else {
                util.message(res.datas)
            }
        })
    },
    getGoodsList:function(){
        var that = this;
        util.fetchPost("/api/store/store/goods_list", {
            page: that.data.goodsPage,
            class_id:that.data.classid
        }, function (res) { 
            if (res.state == 1) {
                var allpost = that.data.goods_list;
                if (allpost == undefined) {
                    allpost = [];
                }
                for (var i in res.datas) {
                    allpost.push(res.datas[i])
                }
                if (res.datas.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '正在加载中...',
                    });
                }
                that.setData({
                    goods_list: allpost,
                    ready: res.datas.length == 0 ? 1 : 2,
                })
            } else {
                util.message(res.datas)
            }
        })
    }, 
    getCommentsList:function(){
        var that = this;
        util.showLoading();
        util.fetchPost("/api/store/store/store_comments_list", {
            page: that.data.comPage,
        }, function (res) {
            if (res.state == 1) {
                // utils.hideLoading();
                var allpost = that.data.commment;
                if (allpost == undefined) {
                    allpost = [];
                }
                for (var i in res.datas) {
                    allpost.push(res.datas[i])
                }
                if (res.datas.length < 10) {
                    that.setData({
                        com_tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        com_tishi_title: '正在加载中......',
                    });
                }
                that.setData({
                    commment: allpost,
                    com_ready: res.datas.length == 0 ? 1 : 2,
                })
            } else {
                util.message(res.datas)
            }
        })
    },
    //tab切换
    changeIndex: function (e) {
        var that = this;
        that.setData({
            curIndex: e.currentTarget.dataset.index
        })
    },
    //店铺分类切换
    swichNav:function(e){
        var that=this;
        that.setData({
            classid: e.currentTarget.dataset.id,
            goodsPage:1,
            goods_list:[]
        })
        that.getGoodsList();

    },
    //编辑商品e
    onDeitTap:function(e){
        wx.navigateTo({
            url: '../deit/deit?goods_id='+e.currentTarget.dataset.id,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
		var that = this;
		app.checkLogin(function () {
			that.setData({
				goodsPage:1,
				goods_list:[],
				comPage:1,
				commment:[],
			});
			that.getStoreInfo();
			that.getClassList();
			that.getCommentsList();
		})
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if(that.data.curIndex == 0){
            if(that.data.ready == 2){
                var page = that.data.goodsPage + 1
                that.setData({
                    goodsPage: page,
                })
                that.getGoodsList();
            }
        }else if(that.data.curIndex == 1){
            if(that.data.com_ready == 2){
                var page = that.data.comPage + 1
                that.setData({
                    comPage: page,
                })
                that.getCommentsList();
            }
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})